# encoding: utf-8
# @File  : tbtest.py
# @Author: Morpheus
# @Date  : 2025/10/31/16:22

import time
import json
from selenium import webdriver

def taobao_login_with_cookie(cookie_file="taobao_cookies.json"):
    """使用cookie登录淘宝"""
    driver = webdriver.Chrome()

    try:
        # 加载cookie
        with open(cookie_file, 'r', encoding='utf-8') as f:
            cookies = json.load(f)

        # 访问淘宝首页
        driver.get("https://www.taobao.com")
        time.sleep(2)

        # 清除并添加cookie
        driver.delete_all_cookies()
        for name, value in cookies.items():
            driver.add_cookie({
                'name': name,
                'value': value,
                'domain': '.taobao.com'
            })

        # 刷新使cookie生效
        driver.refresh()
        time.sleep(2)

        # 简单验证登录状态
        if "亲，请登录" not in driver.page_source:
            print("登录成功")
            return driver
        else:
            print("登录失败")
            return None

    except Exception as e:
        print(f"登录出错: {e}")
        return None

# 使用示例
if __name__ == "__main__":
    driver = taobao_login_with_cookie()

    if driver:
        print("登录成功，可以执行后续操作")
        # 你的操作代码在这里

        input("按回车关闭浏览器...")
        driver.quit()